package com.srit.yoggsaron.controller;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.PropertySource;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.srit.yoggsaron.common.JsonPageResult;
import com.srit.yoggsaron.common.JsonQuery;
import com.srit.yoggsaron.common.JsonResult;
import com.srit.yoggsaron.db.model.User;
import com.srit.yoggsaron.support.UserSupport;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

@Api(tags = "用户相关信息接口")
@RestController
@RequestMapping(value = "/user")
@PropertySource(value="classpath:application.properties" , encoding = "UTF-8")
public class UserController {
	private final Logger logger = LoggerFactory.getLogger(UserController.class);
	@Autowired
	private UserSupport _userSupport=null;
	@ApiOperation(value = "添加用户信息接口", httpMethod = "PUT", notes = "添加用户信息")
	@PostMapping(value = "/add")
	@ResponseBody
	public   JsonResult<Long >  add(@RequestBody  User user) {
		    
		try
		{
			if(user==null)
				throw new Exception("要添加的被用户为空");
			Long  result = _userSupport.insert( user );
			return  new JsonResult< Long>( result).success();
		}
		catch (Exception e) {
			logger.debug(e.toString(),e);
			logger.error(e.toString(),e);
			return new JsonResult<Long>(0L).fail(-1 , "添加被用户失败:  "+e.getMessage() );
		}
	}
	
	
	@ApiOperation(value = "删除被用户接口", 
			httpMethod = "DELETE" ,
			notes = "删除被用户" 
			)
	@ApiImplicitParams({
		@ApiImplicitParam(name="jsonQuery",value="被用户对象",required=true      ) 
	})
	@PostMapping(value = "/remove")
	@ResponseBody
	public  JsonResult<Long > remove ( @RequestBody  JsonQuery jsonQuery   ) {
		try
		{
			if(jsonQuery==null)
				throw new Exception("要删除的被用户条件为空");
			Long  result = _userSupport.delete(  jsonQuery );
			return  new JsonResult< Long>( result).success();
		}
		catch (Exception e) {
			logger.debug(e.toString(),e);
			logger.error(e.toString(),e);
			return new JsonResult<Long>(0L).fail(-1 , "删除被用户失败"+e.getMessage() );
		}
	}
	
	@ApiOperation(value = "修改被用户接口", 
			httpMethod = "POST" ,
			notes = "修改被用户" 
			)
	@ApiImplicitParams({
		@ApiImplicitParam(name="user",value="被用户对象",required=true      ) 
	})
	@PostMapping(value = "/update")
	@ResponseBody
	public  JsonResult<Long > update ( @RequestBody  User user   ) {
		try
		{
			if(user==null|| user.getUserId() ==null)
				throw new Exception("要修改的被用户为空或用户ID为空");
			Long  result = _userSupport.update( user );
			return  new JsonResult< Long>( result).success();
		}
		catch (Exception e) {
			logger.debug(e.toString(),e);
			logger.error(e.toString(),e);
			return new JsonResult<Long>(0L).fail(0 , "修改被用户失败"+e.getMessage() );
		}
	}
	
	
	
	
	@ApiOperation(value = " 查询户接口", 
			httpMethod = "POST" ,
			notes = "查询用户" 
			)
	@ApiImplicitParams({
		@ApiImplicitParam(name="jsonQuery",value="查询条件对象",required=true      ) 
	})
	@PostMapping(value = "/query")
	@ResponseBody
	public  JsonResult<List<User> > query ( @RequestBody   JsonQuery jsonQuery   ) {
		try
		{
			List<User>  result = _userSupport.query( jsonQuery );
			return  new JsonResult< List<User>>( result).success();
		}
		catch (Exception e) {
			logger.debug(e.toString(),e);
			logger.error(e.toString(),e);
			return new JsonResult<List<User>>(null).fail(-1 , "查询用户失败"+e.getMessage() );
		}
	}
	@ApiOperation(value = " 查询户带分页接口", 
			httpMethod = "POST" ,
			notes = "查询带分页用户" 
			)
	@ApiImplicitParams({
		@ApiImplicitParam(name="jsonQuery",value="被用户对象",required=true      ) 
	})
	@PostMapping(value = "/page")
	@ResponseBody
	public  JsonResult<List<User> > page ( @RequestBody   JsonQuery jsonQuery   ) {
		try
		{
			Long count =  _userSupport.count( jsonQuery );
			List<User>  result = _userSupport.page( jsonQuery );
			return  new JsonPageResult< User>( count ,result).success();
		}
		catch (Exception e) {
			logger.debug(e.toString(),e);
			logger.error(e.toString(),e);
			return new JsonResult<List<User>>(null).fail(-1 , "查询用户失败"+e.getMessage() );
		}
	}
	
	
}
